package com.heu.blood.transfusion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.analysis.entity.BloodSampleInfoTransfusionEntity;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.match.entity.BloodMatchEntity;
import com.heu.blood.match.entity.PatientInformationEntity;
import com.heu.blood.match.exception.TimeException;
import com.heu.blood.match.service.BloodInformationService;
import com.heu.blood.match.service.BloodMatchService;
import com.heu.blood.match.service.PatientInformationService;
import com.heu.blood.send.dao.BloodSendDao;
import com.heu.blood.send.entity.BloodSendEntity;
import com.heu.blood.send.service.BloodSendService;
import com.heu.blood.transfusion.dao.BloodAdverseReactionDao;
import com.heu.blood.transfusion.entity.BloodAdverseReactionEntity;
import com.heu.blood.transfusion.entity.BloodAdverseReactionInfo;
import com.heu.blood.transfusion.entity.BloodAdverseReactionVo;
import com.heu.blood.transfusion.entity.TransfusionApplicationEntity;
import com.heu.blood.transfusion.service.BloodAdverseReactionService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("bloodAdverseReactionService")
public class BloodAdverseReactionServiceImpl extends ServiceImpl<BloodAdverseReactionDao, BloodAdverseReactionEntity> implements BloodAdverseReactionService {
    @Autowired
    private BloodAdverseReactionDao bloodAdverseReactionDao;

    @Autowired
    private BloodSendService bloodSendService;
    @Autowired
    private BloodSendDao bloodSendDao;
    @Autowired
    private PatientInformationService patientInformationService;
    @Autowired
    private BloodInformationService bloodInformationService;
    @Autowired
    private BloodMatchService bloodMatchService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 构造BloodAdverseReactionVo对象
        BloodAdverseReactionEntity bloodAdverseReaction = new BloodAdverseReactionEntity(); // 用于接收传递过来的BloodAdverseReactionEntity参数

        if (Strings.isNotBlank((String) params.get("patientId"))){
            bloodAdverseReaction.setPatientId((String) params.get("patientId")); // 添加aboBloodType
        }
        if (Strings.isNotBlank((String) params.get("aboBloodType"))){
            bloodAdverseReaction.setAboBloodType((String) params.get("aboBloodType")); // 添加aboBloodType
        }
        if (Strings.isNotBlank((String) params.get("rhBloodType"))){
            bloodAdverseReaction.setRhBloodType((String) params.get("rhBloodType")); // 添加aboBloodType
        }
        if (Strings.isNotBlank((String) params.get("patientName"))){
            bloodAdverseReaction.setPatientName((String) params.get("patientName"));
        }
        /*if (Strings.isNotBlank((String) params.get("bloodReservoirNumber"))){
            bloodAdverseReaction.setAboBloodType((String) params.get("bloodReservoirNumber"));
        }*/
        if (Strings.isNotBlank((String) params.get("adverseReactionsNo"))){
            bloodAdverseReaction.setAdverseReactionsNo((String) params.get("adverseReactionsNo"));
        }
        if (Strings.isNotBlank((String) params.get("isAdverseReaction"))){
            bloodAdverseReaction.setIsAdverseReaction((String) params.get("isAdverseReaction"));
        }

        if (Strings.isNotBlank((String) params.get("department"))){
            bloodAdverseReaction.setDepartment((String) params.get("department"));
        }


        // 时间格式化，解决Date->String 报错
        String startTime = (String)params.get("startTime");
        String endTime = (String) params.get("endTime");


        if (Strings.isNotBlank(startTime) && Strings.isNotBlank(endTime)) {
            if (startTime.compareTo(endTime) > 0) {
                throw new MyException(TimeException.TIME_OUTLINE);
            }
        }

        // 构造条件构造器对象
        LambdaQueryWrapper<BloodAdverseReactionEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.eq(Strings.isNotBlank(bloodAdverseReaction.getAboBloodType()),BloodAdverseReactionEntity::getAboBloodType,bloodAdverseReaction.getAboBloodType())
                .eq(Strings.isNotBlank(bloodAdverseReaction.getRhBloodType()),BloodAdverseReactionEntity::getRhBloodType,bloodAdverseReaction.getRhBloodType())
                .eq(Strings.isNotBlank(bloodAdverseReaction.getDepartment()),BloodAdverseReactionEntity::getDepartment,bloodAdverseReaction.getDepartment())
                .eq(Strings.isNotBlank(bloodAdverseReaction.getAdverseReactionsNo()),BloodAdverseReactionEntity::getAdverseReactionsNo,bloodAdverseReaction.getAdverseReactionsNo())
                .eq(Strings.isNotBlank(bloodAdverseReaction.getIsAdverseReaction()),BloodAdverseReactionEntity::getIsAdverseReaction,bloodAdverseReaction.getIsAdverseReaction())
                .eq(Strings.isNotBlank(bloodAdverseReaction.getPatientName()), BloodAdverseReactionEntity::getPatientName, bloodAdverseReaction.getPatientName())
                .eq(Strings.isNotBlank(bloodAdverseReaction.getPatientId()), BloodAdverseReactionEntity::getPatientId, bloodAdverseReaction.getPatientId())
                .eq(Strings.isNotBlank(String.valueOf(bloodAdverseReaction.getIfShow())),BloodAdverseReactionEntity::getIfShow,IF_SHOW.code())
                .ge(Strings.isNotBlank(startTime), BloodAdverseReactionEntity::getDispatchTime, startTime)
                .le(Strings.isNotBlank(endTime),BloodAdverseReactionEntity::getDispatchTime, endTime);

        // 根据不良反应时间升序排列
//        queryWrapper.orderByAsc(BloodAdverseReactionEntity::getDispatchTime);
//        IPage<BloodAdverseReactionEntity> page = this.page(
//                new Query<BloodAdverseReactionEntity>().getPage(params),
//                queryWrapper
//        );
        //Query<BloodAdverseReactionVo> query = new Query<>();
        IPage<BloodAdverseReactionEntity> page = this.page(
                new Query<BloodAdverseReactionEntity>().getPage(params),
                queryWrapper
        );
        System.out.println(params);

        return new PageUtils(page);
    }

    @Override
    public List<BloodAdverseReactionInfo> getAdverseReactionInfo(String bloodId) {
        if(bloodId == null || "".equals(bloodId)) return null;
        List<BloodAdverseReactionInfo> res = new ArrayList<>();
        LambdaQueryWrapper<BloodSendEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodId), BloodSendEntity::getBloodId,bloodId);
        List<BloodSendEntity> entities = bloodSendDao.selectList(queryWrapper);

        for (BloodSendEntity entity: entities) {
            BloodAdverseReactionInfo bloodAdverseReactionInfo = new BloodAdverseReactionInfo();
            String patientId = entity.getPatientId();
            List<PatientInformationEntity> patientInformationEntities = patientInformationService.queryListWithPatientId(patientId);
            bloodAdverseReactionInfo.setPatientInformationEntity(patientInformationEntities);
            List<BloodInformationEntity> bloodInformationEntities = bloodInformationService.queryListByBloodId(bloodId);
            bloodAdverseReactionInfo.setBloodInformationEntity(bloodInformationEntities);
           // List<BloodMatchEntity> matchListWithBloodIdAndApplicationItem = bloodMatchService.getMatchListWithBloodIdAndApplicationItem(bloodId, entity.getBloodApplicationItemId());
            List<BloodMatchEntity> matchListWithBloodIdAndApplicationItem =
                    bloodMatchService.getMatchListWithBloodIdAndApplicationItem(
                            bloodId,
                            String.valueOf(entity.getBloodApplicationItemId())  // 显式转换为 String
                    );
            bloodAdverseReactionInfo.setBloodMatchEntity(matchListWithBloodIdAndApplicationItem);
            bloodAdverseReactionInfo.setBloodSendEntity(entity);
            res.add(bloodAdverseReactionInfo);
        }
        return res;
    }

    @Override
    public BloodAdverseReactionEntity getReactionWithBloodId(String bloodId) {
        LambdaQueryWrapper<BloodAdverseReactionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodId),BloodAdverseReactionEntity::getBloodId,bloodId);
        BloodAdverseReactionEntity res = this.getOne(queryWrapper);
        return res;
    }

    @Override
    public List<BloodAdverseReactionEntity> getReactionWithApplicationItemId(Integer applicationItemId) {
        LambdaQueryWrapper<BloodAdverseReactionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodAdverseReactionEntity::getBloodApplicationItemId,applicationItemId);
        List<BloodAdverseReactionEntity> res = this.list(queryWrapper);
        return res;
    }
}